#import <Foundation/Foundation.h>
#import "MTTetrominosPiece.h"

@implementation MTTetrominosPiece : NSObject

-(MTTetrominosPiece*) initWithPiece: (int) piece withColor: (int) color
{
	[self selectShape: piece];
	[self setPieceColor: color];
	return self;
	_orientation = NORMAL;
}

-(MTTetrominosPiece*) initWithPiece: (int) piece
{
	[self selectShape: piece];
	return self;
	_orientation = NORMAL;
}

-(int) originX
{
	switch(_orientation)
	{
		case NORMAL: return 1; break;
		case CLOCKWISE: return 2; break;
		case OPPOSITE: return 2; break;
		case COUNTERCLOCKWISE: return 1; break;
	}
}

-(int) originY
{
	switch(_orientation)
	{
		case NORMAL: return 1; break;
		case CLOCKWISE: return 1; break;
		case OPPOSITE: return 2; break;
		case COUNTERCLOCKWISE: return 2; break;
	}
}

-(int) orientation
{
	return _orientation;
}

-(int) pieceType
{
	return _pieceType;
}

-(int) pieceColor
{
	return _pieceColor;
}

-(int) getMask: (int)x : (int)y
{
	int arrayPos;
	
	switch(_orientation)
	{
		case NORMAL: arrayPos = x + (PIECE_WIDTH * y); break;
		case CLOCKWISE: arrayPos = y - PIECE_WIDTH * x + (PIECE_WIDTH) * (PIECE_HEIGHT - 1); break;
		case OPPOSITE: arrayPos = -x + -y * PIECE_HEIGHT + PIECE_WIDTH * PIECE_HEIGHT - 1; break;
		case COUNTERCLOCKWISE: arrayPos = -y + PIECE_WIDTH * x + PIECE_WIDTH - 1; break;
	}	
	
	return _piece[arrayPos] == 0 ? NOPIECE : _pieceColor;
}

-(int) getPieceType
{
	return _pieceType;
}

-(void) setOrientation: (int)orientation
{
	_orientation = orientation;
}

-(int) getOrientation
{
	return _orientation;
}

-(void) setPieceColor: (int)pieceColor
{
	_pieceColor = pieceColor;
}

-(void) selectShape: (int)shape
{	
	switch(shape)
	{
		case I_SHAPE:
		{
			int lShape[16] = 
				{0,0,0,0,
				 1,1,1,1,
				 0,0,0,0,
				 0,0,0,0};
			memcpy(_piece, lShape, sizeof(int) * 16);
			_pieceColor = CYAN;
			break;
		}
		case J_SHAPE:
		{
			int lShape[16] = 
				{2,0,0,0,
				 2,2,2,0,
				 0,0,0,0,
				 0,0,0,0};
			memcpy(_piece, lShape, sizeof(int) * 16);	 
			_pieceColor = BLUE;
			break;
		}
		case L_SHAPE:
		{
			int lShape[16] = 
				{3,3,3,0,
				 3,0,0,0,
				 0,0,0,0,
				 0,0,0,0};
			memcpy(_piece, lShape, sizeof(int) * 16);	
			_pieceColor = ORANGE;
			break;
		}
		case O_SHAPE:
		{
			int lShape[16] = 
				{0,0,0,0,
				 0,4,4,0,
				 0,4,4,0,
				 0,0,0,0};
			memcpy(_piece, lShape, sizeof(int) * 16);	 
			_pieceColor = YELLOW;
			break;
		}
		case S_SHAPE:
		{
			int lShape[16] = 
				{0,5,5,0,
				 5,5,0,0,
				 0,0,0,0,
				 0,0,0,0};
			memcpy(_piece, lShape, sizeof(int) * 16);	
			_pieceColor = GREEN;
			break;
		}
		case T_SHAPE:
		{
			int lShape[16] = 
				{0,6,0,0,
				 6,6,6,0,
				 0,0,0,0,
				 0,0,0,0};
			memcpy(_piece, lShape, sizeof(int) * 16);	 
			_pieceColor = PURPLE;
			break;
		}
		case Z_SHAPE:
		{
			int lShape[16] = 
				{7,7,0,0,
				 0,7,7,0,
				 0,0,0,0,
				 0,0,0,0};
			memcpy(_piece, lShape, sizeof(int) * 16);	
			_pieceColor = RED; 
			break;
		}
		default:
		{
				int megablock[16] = 
				{1,1,1,1,
				 1,1,1,1,
				 1,1,1,1,
				 1,1,1,1};
			memcpy(_piece, megablock, sizeof(int) * 16);	 
			_pieceColor = RED;
			break;
		}
	}
	
	_pieceType = shape;
}


@end
